/**
 * 
 */
package br.com.ema.maze.character.strategy;

import br.com.ema.maze.agents.MazeCharacter;
import br.com.ema.maze.components.MazeSpace;

/**
 * @author Emanuel Cruz Rodrigues -> emanuelcruzrodrigues@gmail.com
 *
 */
public class AStarStrategy implements MazeCharacterStrategy{
	
	private long memoryInMiliseconds;
	
	public AStarStrategy(long memoryInMiliseconds) {
		super();
		this.memoryInMiliseconds = memoryInMiliseconds;
	}

	/* (non-Javadoc)
	 * @see br.com.ema.maze.character.strategy.MazeCharacterStrategy#executeStrategy(br.com.ema.maze.agents.MazeCharacter)
	 */
	@Override
	public void executeStrategy(MazeCharacter mazeCharacter) {
		/*
		 * get the destination and the actual space
		 */
		MazeSpace destination = mazeCharacter.getDestination();
		if (destination == null) return;
		MazeSpace actualSpace = mazeCharacter.getActualSpace();
		if (actualSpace == null) return;

		/*
		 * calculates the next step
		 */
		MazeSpace newSpace = actualSpace.getMinOriginalRoute(mazeCharacter, destination, memoryInMiliseconds);
		if (newSpace == null) return;

		/*
		 * update the actual step
		 */
		actualSpace.removeCharacter(mazeCharacter);

		/*
		 * setup the new step
		 */
		mazeCharacter.setActualSpace(newSpace);
		newSpace.putCharacter(mazeCharacter);


	}

}
